package com.yanqu.road.server.manger.rank;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.logic.pb.YRankPb;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class YRankMgr extends TempMgr {

    private static final Map<Integer, Map<Integer, Map<String, Map<Long, UserYRank>>>> userYRankCacheMap = new ConcurrentHashMap<>();

    public static void addUserYRankCache(UserYRank userYRank) {
        synchronized (userYRankCacheMap) {
            userYRankCacheMap.putIfAbsent(userYRank.getActivityId(), new ConcurrentHashMap<>());
            userYRankCacheMap.get(userYRank.getActivityId()).putIfAbsent(userYRank.getGroupId(), new ConcurrentHashMap<>());
            userYRankCacheMap.get(userYRank.getActivityId()).get(userYRank.getGroupId()).putIfAbsent(userYRank.getIdKey(), new ConcurrentHashMap<>());
            userYRankCacheMap.get(userYRank.getActivityId()).get(userYRank.getGroupId()).get(userYRank.getIdKey()).put(userYRank.getUserId(), userYRank);
        }
    }

    public static void uploadUserYRank() {
        Map<Integer, Map<Integer, Map<String, Map<Long, UserYRank>>>> map;
        synchronized (userYRankCacheMap) {
            map = new ConcurrentHashMap<>(userYRankCacheMap);
            userYRankCacheMap.clear();
        }

        for (Map.Entry<Integer, Map<Integer, Map<String, Map<Long, UserYRank>>>> entry : map.entrySet()) {
            int activityId = entry.getKey();
            for (Map.Entry<Integer, Map<String, Map<Long, UserYRank>>> entry2 : entry.getValue().entrySet()) {
                int groupId = entry2.getKey();
                for (Map.Entry<String, Map<Long, UserYRank>> entry3 : entry2.getValue().entrySet()) {
                    String idKey = entry3.getKey();

                    YRankProto.UserYRankListMsg.Builder listBuilder = YRankProto.UserYRankListMsg.newBuilder();
                    listBuilder.setActivityId(activityId);
                    listBuilder.setGroupId(groupId);
                    listBuilder.setIdKey(idKey);
                    for (UserYRank userYRank : entry3.getValue().values()) {
                        listBuilder.addRankList(YRankPb.parseUserYRankMsg(userYRank));
                    }

                    GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_UPLOAD_USER_RANK, listBuilder, 0, activityId, groupId);
                }
            }
        }
    }

    public static void uploadUnionYRank(UserYRank userYRank, String unionUid, int changeType) {
        // user && union
        YRankProto.UnionYRankUserMsg.Builder builder = YRankProto.UnionYRankUserMsg.newBuilder();
        builder.setUser(YRankPb.parseUserYRankMsg(userYRank));
        builder.setUnionUid(unionUid);
        UnionBaseInfo unionBaseInfo = CrossUnionMgr.parseUnionBaseInfo(unionUid);
        if (unionBaseInfo != null) {
            builder.setUnionBaseInfo(UnionBasePb.parseUnionBaseTempMsg(unionBaseInfo));
        }else {
            builder.setUnionBaseInfo(UnionProto.UnionBaseTempMsg.newBuilder());
        }
        Map<Long, Integer> positionMap = CrossUnionMgr.getMemberPositionMap(unionUid);
        for (Map.Entry<Long, Integer> entry : positionMap.entrySet()) {
            builder.addPosList(YRankProto.UnionYRankPosMsg.newBuilder().setUserId(entry.getKey()).setPos(entry.getValue()));
        }
        builder.setChangeType(changeType);
        // list
        YRankProto.UnionYRankUserListMsg.Builder listBuilder = YRankProto.UnionYRankUserListMsg.newBuilder();
        listBuilder.setActivityId(userYRank.getActivityId());
        listBuilder.setGroupId(userYRank.getGroupId());
        listBuilder.setIdKey(userYRank.getIdKey());
        listBuilder.addRankList(builder);
        // send
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_UPLOAD_UNION_RANK_USER, listBuilder, userYRank.getUserId(), userYRank.getActivityId(), userYRank.getGroupId());
    }

    public static void uploadUnionYRank(UserActivityConditionData userActivityConditionData, String unionUid, int changeType) {
        long userId = userActivityConditionData.getUserId();
        int activityId = userActivityConditionData.getActivityId();
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, unionUid);
        if (StringUtils.isNullOrEmpty(unionUid)) {
            //去拿一下活动消息
            ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(activityId);
            //如果是需要分组的活动 没商会不给玩 不给上传了 锁名单版本
            if (openActivityInfo == null || UnionActivityGroupHelper.needGroup(openActivityInfo)){
                return;
            }else {
                //应该是0了
                groupId = GameUnionActivityGroupMgr.getGroupId(activityId,userId);
            }
        }
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            return;
        }

        UserBaseInfo userBaseInfo = PlayerBasePb.parseBaseUserInfo(PlayerPb.parsePlayerBaseTempMsg(UserMgr.getUserInfo(userId)).build());

        UserYRank userYRank = new UserYRank();
        userYRank.setActivityId(activityId);
        userYRank.setGroupId(groupId);
        userYRank.setIdKey(String.valueOf(userActivityConditionData.getType()));
        userYRank.setUserId(userId);
        userYRank.setUserBaseInfo(userBaseInfo);
        userYRank.setValue(userActivityConditionData.getValue());
        userYRank.setParam(userActivityConditionData.getParam());
        userYRank.setUpdateTime(System.currentTimeMillis());

        uploadUnionYRank(userYRank, unionUid, changeType);
    }

    public static void uploadUnionYRank(int activityId, String idKey, String unionUid, String param) {
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, unionUid);
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            return;
        }

        YRankProto.UnionYRankUpdateReqMsg.Builder builder = YRankProto.UnionYRankUpdateReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setGroupId(groupId);
        builder.setIdKey(idKey);
        builder.setUnionUid(unionUid);
        builder.setUnionBaseInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionUid)));
        Map<Long, Integer> positionMap = CrossUnionMgr.getMemberPositionMap(unionUid);
        for (Map.Entry<Long, Integer> entry : positionMap.entrySet()) {
            builder.addPosList(YRankProto.UnionYRankPosMsg.newBuilder().setUserId(entry.getKey()).setPos(entry.getValue()));
        }
        if (param != null) {
            builder.setParam(param);
        }
        // send
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_UPDATE_UNION_RANK, builder, 0, activityId, groupId);
    }

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
